[C#] Is variable assignment and reading atomic operation (threading)
Posted
by AStrangerGuy
on Stack Overflow
See other posts from Stack Overflow
or by AStrangerGuy
Published on 2010-04-29T10:28:10Z
Indexed on
2010/04/29
10:47 UTC
Read the original article
Hit count: 688
I was unable to find any reference to this in the documentations...
Is assigning to a double (or any other simple type, including boolean) an atomic operation viewed from the perspective of threads?
double value = 0;
public void First() {
while(true) {
value = (new Random()).NextDouble();
}
}
public void Second() {
while(true) {
Console.WriteLine(value);
}
}
In this code sample, first method is called in one thread, and the second in another. Can the second method get a messed up value if it gets its execution during assignment to the variable in another thread?
I don't care if I recieve the old value, it's only important to receive a valid value (not one where 2 out of 8 bytes are set). I know it's a stupid question, but I want to be sure, cause I don't know how CLR actually sets the variables.
Thanks
© Stack Overflow or respective owner